﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class pages_Default : System.Web.UI.Page
{
    public static string WzType;
    public static string Type;
    public static int WzID;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            txtAmount.Attributes["onkeyup"] = "value=value.replace(/[^\\d]/g,'')";//设置txtAmount的输入文本只能为数字
            txtUnitPrise.Attributes["onkeyup"] = "value=value.replace(/[^\\d]/g,'')";//设置txtAmount的输入文本只能为数字
            Bind();
            if (Type == "")
            {
                DB.TYPE_SECTION = "物资大类";
                Response.Write("<script>alert('尚无可选的物资大类，请先设置！');location='EditBigType.aspx';</script>");
            }
            else if (WzType == "")
            {
                DB.TYPE_SECTION = "物资类别";
                Response.Write("<script>alert('尚无可选的物资类别，请先设置！');location='MaterialType.aspx';</script>");
            }
            else
            {
                this.AutoID();
            }
            
            this.Label1.Text = DB.TYPE_SECTION;
        }
    }
    public void AutoID()
    {
       string cmdText = "select Sequence from tb_Type where Type='" + Type + "'and Title='" + WzType + "'";
       DataTable dtType= DB.ExcuteDataTable(cmdText);
       string sequence = dtType.Rows[0]["Sequence"].ToString();
       cmdText = "select maxID=max(WzID) from tb_Material Where WzType='"+WzType+"'";
       DataTable dtMaterial = DB.ExcuteDataTable(cmdText);
       string maxID = dtMaterial.Rows[0]["maxID"].ToString();
       if (maxID == "")
       {
           this.txtID.Text = sequence + "1";
       }
       else
       {
           this.txtID.Text = sequence + (Convert.ToInt32 (maxID.Substring(sequence.Length, maxID.Length - sequence.Length))+1);
       }
        
    }
    public void Bind()
    {
        this.ddlBigTypeBind();
        this.ddlTypeBind();
        gvMaterialBind();
        DataTable ds4 = DB.ExcuteDataTable("select * from tb_BigType where Type='计量单位'");
        ddlUnit.DataSource = ds4;
        ddlUnit.DataTextField = "Title";
        this.ddlUnit.DataBind();
    }
    public void ddlBigTypeBind()
    {
        DataTable dt1 = DB.ExcuteDataTable("SELECT DISTINCT ID, Title FROM tb_BigType WHERE Type='物资大类'");
        if (dt1.Rows.Count == 0)
        {
            Type = "";
        }
        else
        {
            this.ddlBigType.DataSource = dt1;
            this.ddlBigType.DataTextField = "Title";
            Type = dt1.Rows[0]["Title"].ToString();
            ddlBigType.DataBind();
        }
    }
    public void ddlTypeBind()
    {
        DataTable dt2 = DB.ExcuteDataTable("SELECT DISTINCT ID, Title FROM tb_Type WHERE Type='" + Type + "'");
        if (dt2.Rows.Count == 0)
        {
            WzType = "";
        }
        else
        {
            this.ddlType.DataSource = dt2;
            this.ddlType.DataTextField = "Title";
            WzType = dt2.Rows[0]["Title"].ToString();
            ddlType.DataBind();
        }
    }
    public void gvMaterialBind()
    {
        DataTable dt3 = DB.ExcuteDataTable("SELECT DISTINCT * FROM tb_Material WHERE WzType='" + WzType + "'");
        gvMaterial.DataSource = dt3;
        gvMaterial.DataBind();
    }
    //初始化控件
    public void clear()
    {
        txtID.Text = "";
        txtName.Text = "";
        txtMode.Text = "";
        txtSpec.Text = "";
        txtCard.Text = "";
        txtUnitPrise.Text = "";
        ddlUnit.SelectedIndex = -1;
        txtAmount.Text = "";
        RadioButtonList1.Items[0].Selected = true;
    }
    protected void lbtnDelete_Load(object sender, EventArgs e)
    {
        //在页面加载时，给ID属性为lbtnDelete的LinkButton控件加上onclick事件，弹出警告对话框
        ((LinkButton)sender).Attributes["onclick"] = "javascript:return confirm('你确认要删除选中项吗？')";
    }
    protected void lbtnDelete_Command(object sender, CommandEventArgs e)
    {
        //获取lbtnDelete的LinkButton对象
        LinkButton lbtn = (LinkButton)sender;
        GridViewRow row = (GridViewRow)lbtn.Parent.Parent;//引用lbtnDelete控件的父控件上一级控件
        //取得该控件的Text值
        string id = ((DataControlFieldCell)gvMaterial.Rows[row.RowIndex].Cells[1]).Text.Trim();
        DB.ExSql("delete from tb_Material where WzID='" + id + "'");
        gvMaterial.Rows[row.RowIndex].Visible = false;//在不重新绑定GridView控件的条件下，将删除的行先隐藏
        this.gvMaterialBind();
        AutoID();
    }
   protected void btnInsert_Click(object sender, EventArgs e)
    {
        string ID = txtID.Text.Trim();
        string Name = txtName.Text.Trim();
        string Mode = txtMode.Text.Trim();
        string Spec = txtSpec.Text.Trim();
        string Card = txtCard.Text.Trim();
        int UnitPrise = Convert.ToInt32(txtUnitPrise.Text.Trim());
        string Unit = ddlUnit.SelectedItem.Text.Trim();
        int Amount = Convert.ToInt32(txtAmount.Text.Trim());
        string Type = RadioButtonList1.SelectedItem.Text.Trim();
        if (Type == "是")
        {
            Type = "固定资产";
        }
        else
        {
            Type = "非固定资产";
        }
        string cmdText = "insert into tb_Material values ('" + ID + "','" +WzType + "','"+Name+"','" + Mode + "','" + Spec + "','" + Card + "'," + UnitPrise + ",'" + Unit + "'," + Amount + ",'" + Type + "')";
        DB.ExSql(cmdText);
        gvMaterialBind();
        this.clear();
        AutoID();
    }
    protected void gvMaterial_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        this.gvMaterial.PageIndex = e.NewPageIndex;
        this.gvMaterialBind();
    }

    protected void ddlBigType_SelectedIndexChanged(object sender, EventArgs e)
    {
        Type =this.ddlBigType.Text;
        DataTable dt2 = DB.ExcuteDataTable("select distinct ID, Title from tb_Type where Type='" + Type + "'");
        ddlType.DataSource = dt2;
        ddlType.DataTextField = "Title";
        ddlType.DataBind();
        WzType = this.ddlType .Text;

        DataTable dt3 = DB.ExcuteDataTable("select distinct  * from tb_Material where WzType='" + WzType + "'");
        gvMaterial.DataSource = dt3;
        gvMaterial.DataBind();
        if (WzType == "")
        {
            DB.TYPE_SECTION = "物资类别";
            Response.Write("<script>alert('尚无可选的物资类别，请先设置！');location='MaterialType.aspx';</script>");
        }
        else
        {
            this.AutoID();
        }
    }
    protected void ddlType_SelectedIndexChanged(object sender, EventArgs e)
    {
        WzType =this.ddlType.Text;
        gvMaterial.DataSource = DB.ExcuteDataTable("select distinct  * from tb_Material where WzType='" + WzType + "'");
        gvMaterial.DataBind();
        this.AutoID();
    }
}
